<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:付款单
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class PaymentsAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
		//$map['wahname|egname|wahcode|wahaddr'] = array('like',"%".$_POST['selectname']."%");
        
	}
   //页面加载前生产单据编号
    function _before_index(){
        $newCode = $this->getSerialNum('FK','Fundsmain');
        
        $this->assign ( 'newCode', $newCode );
        $this->assign ( 'dtime', date('Y-m-d') );
    }
    
    function addOrders(){
        if($_POST['ordernumber']){$mainData['ordernumber'] = $_POST['ordernumber'];}else{$this->error('单据编号不能为空。');exit;}//單據編號
        if($_POST['receivename_id']){$mainData['receivename'] = $_POST['receivename_id'];}else{$this->error('供货单位不能为空。');exit;}//发货单位
        if($_POST['ordertime']){$mainData['ordertime'] = strtotime($_POST['ordertime']);}else{$this->error('单据日期不能为空。');exit;}//單據日期        
        if($_POST['orderUser_id']){$mainData['orderuser'] = $_POST['orderUser_id'];}else{$this->error('经办人不能为空。');exit;}//经办人
        if($_POST['bankid_id']){$mainData['bankid'] = $_POST['bankid_id'];}else{$this->error('接收仓库不能为空。');exit;}//接收仓库        
        $mainData['remarks'] = $_POST['remarks'];//摘要
        $mainData['payamount'] = $_POST['total_amount'];//应付金额
        $mainData['trueamount'] = $_POST['trade_amount'];//实付金额
        $mainData['createuser'] = $_POST['createManid'];//制单人
        $mainData['ordertype'] = 6;//設置為付款单
        $mainData['status'] = 0;//设置为未生效
        $mainData['dealstatus'] = 0;//设置为未处理
        $mainData['createtime'] = strtotime(date('Y-m-d H:i:s'));//设置为未处理
        $mainData['updatetime'] = strtotime(date('Y-m-d H:i:s'));//设置为未处理
        //$a = $items['orgNumber'];
        
        
        $aProidData = $_POST['items_id'];
        $countRow = count($aProidData);
        if($countRow==0){$this->error('单据没有添加任何商品信息。');exit;}
        $aTotalData = $_POST['items_orgTotal'];
        $aMarksData = $_POST['items_orgMark'];
        
                
        
              
        $saveModel = D('Fundsmain');        
        $map['ordernumber'] = $_POST['ordernumber'];
        if(($saveModel->where($map)->count('id'))>0){
            $this->error('该单据已提交过,请勿重复提交单据。');
        }else{
            if($saveModel->add($mainData)){
                $detailModel = D('Fundsdetails');      
                    //循环本单据每笔单据信息写入数据库
                    for($i=0;$i<$countRow;$i++){
                        $sucsCount = 0;
                        unset($detailData);
                        $detailData['ordersort']   = $i+1;
                        $detailData['ordernumber'] = $_POST['ordernumber'];
                        $detailData['costid']      = $aProidData[$i];
                        $detailData['payamount']   = $aTotalData[$i];
                        $detailData['remarks']     = $aMarksData[$i];
                        $detailData['iotype']      = 1;//1为入库2为出库
                        if($detailModel->add($detailData)){
                        $sucsCount = $sucsCount+1;
                        }
                    }                    
                if($sucsCount = $countRow){ 
                    $this->assign ( "jumpUrl", $this->getReturnUrl());
                   $this->success('单据[ '.$_POST['ordernumber'].' ]已提交,请至[财务单据池]处理单据状态。'); 
                }else{
                   $saveModel->where($map)->delete();
                   $detailModel->where($map)->delete();
                   $this->error('单据[ '.$_POST['ordernumber'].' ]提交失敗,详细信息写入失败。');                   
                }                
            }else{
                $this->error('单据保存失败。');
            }
        }       
        //$this->success($a);
    }

    
 
}
?>